package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.entity.Permission;
import com.orientation.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2021-10-11
 */
@Api(tags = "权限")
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @ApiOperation("添加")
    @PostMapping
    public boolean save(@RequestBody Permission permission) {
        return permissionService.save(permission);
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Permission permission) {
        return permissionService.updateById(permission);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return permissionService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Permission findById(@PathVariable String id) {
        return permissionService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Permission> findAll() {
        return permissionService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Permission> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                      @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<Permission> page = permissionService.page(new Page<>(pageNum, pageSize), Wrappers.<Permission>lambdaQuery().like(Permission::getName, name));
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            for (Permission p : page.getRecords()) {
                for (Permission permission : page.getRecords()) {
                    if (!StringUtils.isEmpty(p.getPid()) && p.getPid().equals(permission.getSid())) {
                        p.setPidName(permission.getName());
                    }
                }
            }
        }
        return page;
    }

    @ApiOperation("查询单条")
    @GetMapping("findByIdPid/{id}")
    public Permission findByIdPid(@PathVariable String id) {
        Permission permission = permissionService.getById(id);
        if (!ObjectUtils.isEmpty(permission) && !StringUtils.isEmpty(permission.getPid())) {
            permission.setPidName(permission.getName());
        }
        return permission;
    }

    @ApiOperation("一级模块")
    @GetMapping("getAll")
    public List<Permission> getAll() {
        return permissionService.list(Wrappers.<Permission>lambdaQuery().isNull(Permission::getPid));

    }

}

